#include "stdafx.h"
#include "CppUnitTest.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace std;

namespace NativeTest_Question23
{
	typedef struct node{
		int val;
		node* Next;
	} node;

	node* DeleteNode(node* list, int val)
	{
		while(list && list->val == val)
			list =list->Next;
		node* walker = list;

		while(walker && walker->Next)
		{
			if(walker->Next->val == val)
			{
				walker->Next = walker->Next->Next;
			}
			else
			{
				walker= walker->Next;
			}
		}
		return list;
	}
	void AssertList(const node* actual, const int* expected,int count)
	{
		for (int i = 0; i < count; i++)
		{      
			Assert::AreEqual(expected[i], actual->val);
			actual = actual->Next;
		}
	}
}
namespace NativeTest
{
	using namespace NativeTest_Question23;

	TEST_CLASS(Question23)
	{
	public:

		TEST_METHOD(Question23_TestMethod1)
		{
			int n = 10;
			int a[] = {  3, 2, 1, 3, 5, 6, 2, 6, 3, 1};
			node* data, *head;

			for (int i = 0; i < n; i++)
			{
				node* tn = new node();
				tn->val= a[i];
				tn->Next = NULL;
				if(i == 0) { head = tn; data = tn;}
				else { data->Next = tn, data = tn;}
			}

			data = head;
			AssertList(data, a, 10);

			data = DeleteNode(head,3);

			int expected1[] = {2,1,5,6,2,6,1};
			AssertList(data, expected1, 7);

			int expected2[] = {2,5,6,2,6};
			data = DeleteNode(data,1);
			AssertList(data, expected2, 5);
		}

	};
}